﻿using Application.Utils;
using Infrastructure.WebApi;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Web.Middlewares;

namespace Web.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ManagerController : ControllerBase
    {
        public IManagerService _service { get; set; }
        public ManagerController(IManagerService service)
        {
            _service = service;
        }
        #region 登录
        [OperationLogTag(title: "登录系统")]
        [HttpPost, AllowAnonymous]
        public async Task<UserLoginResponse> Login([FromBody] UserLoginRequest request)
        {
            return await _service.Login(request, Request.HttpContext.RealIP());
        }
        [HttpPost, AllowAnonymous]
        public async Task<RefreshTokenResponse> RefreshToken([FromBody] RefreshTokenRequest request)
        {
            var access_token = Request.Headers["Authorization"].ToString();
            var res = await _service.RefreshToken(request, access_token);
            return res;
        }
        #endregion

        #region 人员
        [HttpGet]
        public async Task<Page<UserInfo>> Get([FromQuery] UserRequest request)
        {
            return await _service.Get(request);
        }
        [HttpPost]
        public async Task Add([FromBody] CreateManagerRequest request)
        {
            await _service.Add(request);
        }
        [HttpPut]
        public async Task Update([FromBody] UserEditRequest request)
        {
            await _service.Update(request);
        }
        [HttpDelete]
        public async Task Delete([FromQuery] IdRequest request)
        {
            await _service.Delete(request);
        }
        [HttpPut, AnonymousRole, OperationLogTag(title: "修改密码", recordBody: RecordBody.none)]
        public async Task UpdatePassword([FromBody] KeyRequest request)
        {
            await _service.UpdatePassword(request);
        }

        #endregion

        #region 角色
        [HttpGet, AnonymousRole]
        public List<SysRole> GetRoles()
        {
            return _service.GetRoles();
        }
        #endregion
    }
}